﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class SearchResult : System.Web.UI.Page
{
    private DBArticles _searchArt = new DBArticles();

    public string MenuSearchArt = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["isLogIn"].Equals("true"))
        {
            Login.Visible = false;
            TxtLogin.Visible = false;
            Password.Visible = false;
            TxtPassword.Visible = false;
            Log_In.Visible = false;
            LblError.Visible = false;
            LinkRegistration.Visible = false;
            LblIsLogIn.Visible = true;
            LblIsLogIn.Text = "Jesteś zalogowany jako " + Session["login"];
            Log_Out.Visible = true;
        }
        else
        {
            Login.Visible = true;
            TxtLogin.Visible = true;
            Password.Visible = true;
            TxtPassword.Visible = true;
            Log_In.Visible = true;
            LinkRegistration.Visible = true;
            Log_Out.Visible = false;
            LblError.Visible = false;
            LblIsLogIn.Visible = false;
        }
        
        //LblSearchResult.Text = Request.QueryString["SearchField"];

        _searchArt.ClearArticles();

        string searchResult = Request.QueryString["SearchField"];
        if (searchResult != null)
        {
            var sql = "SELECT ArticleID, Title, Description, Image FROM Articles WHERE Title LIKE '%" + searchResult + "%' OR Content LIKE '%" + searchResult + "%'";

            SqlDataReader readerSearchResult = DataBase.SelectData(sql);

            while (readerSearchResult.Read())
            {
                Article art = new Article(readerSearchResult.GetInt32(0), readerSearchResult.GetString(1),
                                          readerSearchResult.GetString(2), readerSearchResult.GetString(3));
                
                _searchArt.AddArticle(art);
            }
            readerSearchResult.Close();
            _setSearchArt();
        }
        //else
        //{
        //    LblSearchResult.Visible = true;
        //}
    }

    private void _setSearchArt()
    {
        List<Article> searchArt = _searchArt.Articles;

        for (int i = 0; i < searchArt.Count; i++)
        {
            Response.Write(searchArt.ElementAt(i).ArticleID);

            LblSearchResult.Visible = true;
            //MenuSearchArt += LblSearchResult.Text;
            MenuSearchArt += "<a class=\"search_article\" href='ArticlePage.aspx?id=" + searchArt.ElementAt(i).ArticleID + "'>";
            MenuSearchArt += "<div class=\"search_title\">" + searchArt.ElementAt(i).Title + "</div><br>";
            MenuSearchArt += "<div class=\"search_image\">" + searchArt.ElementAt(i).Image + "</div>";
            MenuSearchArt += "<div class=\"search_desc\">" + searchArt.ElementAt(i).Description + "</div>";
            MenuSearchArt += "</a> <br>";
        }
    }

    protected void Log_In_Click(object sender, EventArgs e)
    {
        string login = TxtLogin.Text;
        string password = TxtPassword.Text;

        var sql = "SELECT COUNT(*) FROM Users WHERE login LIKE '" + login + "' AND password LIKE '" + password + "'";

        SqlDataReader readerUsers = DataBase.SelectData(sql);

        readerUsers.Read();

        if (login.Equals("admin") & password.Equals("admin"))
        {
            readerUsers.Close();
            Response.Redirect("AdministrationPanel.aspx");
        }

        //moze byc kilka takich samych loginow i hasel
        if (readerUsers.GetInt32(0) >= 1)
        {
            Session["login"] = login;
            Session["isLogIn"] = "true";
            readerUsers.Close();
            Response.Redirect("UserProfile.aspx");
            Login.Visible = false;
            TxtLogin.Visible = false;
            Password.Visible = false;
            TxtPassword.Visible = false;
            Log_In.Visible = false;
            LblError.Visible = false;
            LinkRegistration.Visible = false;
            LblIsLogIn.Visible = true;
            LblIsLogIn.Text = "Jesteś zalogowany jako " + Session["login"];
            Log_Out.Visible = true;
        }
        else
        {
            readerUsers.Close();
            Login.Visible = true;
            TxtLogin.Visible = true;
            Password.Visible = true;
            TxtPassword.Visible = true;
            Log_In.Visible = true;
            LinkRegistration.Visible = true;
            Log_Out.Visible = false;
            LblError.Visible = true;
            TxtLogin.Text = "";
            TxtPassword.Text = "";
        }

        readerUsers.Close();
    }

    protected void Log_Out_Click(object sender, EventArgs e)
    {
        Session["isLogIn"] = "false";
        Response.Redirect("Default.aspx");
        Login.Visible = true;
        TxtLogin.Visible = true;
        Password.Visible = true;
        TxtPassword.Visible = true;
        Log_In.Visible = true;
        LinkRegistration.Visible = true;
        Log_Out.Visible = false;
        LblIsLogIn.Visible = false;
        LblIsLogIn.Text = "";
        TxtLogin.Text = "";
        TxtPassword.Text = "";
    }

    protected void LinkRegistration_Click(object sender, EventArgs e)
    {
        Response.Redirect("Registration.aspx");
    }

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
        //string url = String.Format("~/SearchResult.aspx?SearchField={0}", TxtSearch.Text);
        //Response.Redirect(url);
        string searchWord = TxtSearch.Text;
        if (!searchWord.Equals(""))
        {
            Response.Redirect("SearchResult.aspx?SearchField=" + searchWord);
        }
    }

    protected void LinkLogo_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
}